package java结构型设计模式.代理模式;

public class Main {
    public static void main(String[] args) {
        Printer printer = new ProxyPrinter("test");
        printer.print();
    }
}
interface Printer{
    void print();
}
class ConsolePrinter implements Printer{
    private String fileName;

    public ConsolePrinter(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void print() {
        System.out.println("Displaying    " + fileName);
    }
}
class ProxyPrinter implements Printer{
    private ConsolePrinter consolePrinter;
    private String fileName;

    public ProxyPrinter(String fileName) {
        this.fileName = fileName;
    }

    @Override
    public void print() {
        if (consolePrinter == null){
            consolePrinter = new ConsolePrinter(fileName);
        }
        consolePrinter.print();
    }
}
